<?php

class ManageDoEditCustomerAction extends LtAction
{
	public function __construct()
	{
		parent::__construct();
		$this->responseType = 'tpl';
		$this->layout = 'bylayout';

		$this->data['headnavbar']['visitlog'] = '';
		$this->data['headnavbar']['hyperlink'] = '';
		$this->data['headnavbar']['customer'] = '';
		$this->data['headnavbar']['index'] = '';
	}

	public function execute()
	{
		$this->data['roleinfo'] = ManagerRole::getRoleInfFromCookie(C('LtUrl')->generate('manage','Customer'));
		$cid = '';
		$noticestr = '';
		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			$postdata = $this->context->post('postdata');
			$cid=$postdata['cid'];
			if(!empty($cid))
			{
				$updata = $postdata;
				unset($updata['cid']);
				$tmcustomer = new CustomerDao();
				var_dump($updata);
				$upre = $tmcustomer->editByid($cid, $updata);
				if($upre)
				{
					$noticestr = "succ to do edit";
				}
				else
				{
					$noticestr = "fail to do edit";
				}
			}
		}
		else
		{
			$cid = $this->context->get('cid');
			$noticestr = $this->context->get('noticestr');
		}

		if(empty($cid))
		{
			$this->doRedirect("fail to get cid to load edit page");
		}
	
		$this->data['cid'] = $cid;
		$this->data['title'] = 'edit Cutstomer';
		if(!empty($noticestr))
		{
			$noticestr = "<div class='warning'>{$noticestr}</div>";
		}
		$this->data['noticestr'] = $noticestr;

		$this->data['actionFilePath'] = __FILE__;

		$tmcustomer = new CustomerDao();
		$queryre = $tmcustomer->getByid($cid);//$queryre = array(0=>$queryre);
		if(empty($queryre))
		{	
			$this->doRedirect("fail to custom with cid=$cid");
		}
		
		$this->template = "Manage-Edit-Customer";
		$this->data['queryre'] = $queryre;

		$tmpvlog = new VisitlogDao();
		$queryre = $tmpvlog->getList("cid=:cid", array("cid"=>$cid), 'itime desc');
		//var_dump($queryre);exit;
		$this->data['vlog'] = $queryre;

		$this->data['headnavbar']['customer'] = 'selected';
	}

	private function doRedirect($thenotice)
	{
		$noticestr = "<div class='warning'>{$thenotice}</div>";
		$redirectUrl = C('LtUrl')->generate('manage','index');
		$redirectUrl = "{$redirectUrl}&noticestr=".rawurlencode($noticestr);
		header('Location: '.$redirectUrl);
	}
}
